package com.cloud.wms.ui.group.obtain;

import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.core.eventBus.Event;
import com.cloud.core.utils.TimeUtils;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.EventConfig;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.group.contract.GroupObtainContract;
import com.cloud.wms.ui.group.presenter.GroupObtainPresenter;
import com.cloud.wms.ui.main.entity.CommodityEntity;
import com.cloud.wms.ui.main.entity.TaskEntity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author:Chen
 * @Date: 2021-03-10 10:24
 * @Description:
 */
public class ObtainInventoryActivity extends BaseBarListActivity<CommodityEntity, GroupObtainPresenter> implements GroupObtainContract.View {
    @BindView(R.id.ll_btn)
    LinearLayout llBtn;
    private long billId;
    private TaskEntity taskEntity;

    @Override
    protected String barTitle() {
        return "领用清点";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_group_obtain_inventory;
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_group_obtain_inventory;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        taskEntity = getIntent().getParcelableExtra(IntentKey.TASK_KEY);
        billId = getIntent().getLongExtra(IntentKey.ID_KEY, 0);

        mPresenter.loadBatchCommodity(taskEntity == null ? billId : taskEntity.getBillID());
    }

    @Override
    public void onReload() {
        super.onReload();
        mPresenter.loadBatchCommodity(taskEntity == null ? billId : taskEntity.getBillID());
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, CommodityEntity item) {
        helper.setText(R.id.tv_commodity_code, item.getBarCode())
                .setText(R.id.tv_commodity_name, item.getCommodityName())
                .setText(R.id.tv_pos_code, item.getPosCode())
                .setText(R.id.tv_commodity_amount, item.getAmount() + "");

    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onSuccess(Message message) {
        switch (message.what) {
            case GroupObtainPresenter.BATCH_DETAIL:
                llBtn.setVisibility(View.VISIBLE);
                mAdapter.setNewInstance(((ArrayList<CommodityEntity>) message.obj));
                break;
        }
    }

    @OnClick(R.id.btn_confirm)
    public void onViewClicked() {
        ObtainBindActivity.start(this, taskEntity, billId);
    }

    @Override
    protected boolean isUseEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onEventCome(Event event) {
        if (event == null) {
            return;
        }
        switch (event.getCode()) {
            case EventConfig.CLOSE_REFRESH:
                this.finish();
                break;
        }
    }

    public static void start(Context context, TaskEntity taskEntity, long billId) {
        Intent intent = new Intent(context, ObtainInventoryActivity.class);
        intent.putExtra(IntentKey.TASK_KEY, taskEntity);
        intent.putExtra(IntentKey.ID_KEY, billId);
        context.startActivity(intent);
    }
}
